/**
 * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.plugin.activerecord.generator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.moceit.util.Mc;
import cn.moceit.view.MVViewMode;

/**
 * TableMeta
 */
@SuppressWarnings("serial")
public class TableMeta implements Serializable {

	public String name; // 表名
	public String remarks; // 表备注
	public String primaryKey; // 主键，复合主键以逗号分隔
	public Set<ColumnMeta> columnMetas = new HashSet<ColumnMeta>(); // 字段
																	// meta

	// ---------

	public String baseModelName; // 生成的 base model 名
	public String baseModelContent; // 生成的 base model 内容

	public String modelName; // 生成的 model 名
	public String modelContent; // 生成的 model 内容

	// ---------

	public int colNameMaxLen = "Field".length(); // 字段名最大宽度，用于辅助生成字典文件样式
	public int colTypeMaxLen = "Type".length(); // 字段类型最大宽度，用于辅助生成字典文件样式
	public int colDefaultValueMaxLen = "Default".length(); // 字段默认值最大宽度，用于辅助生成字典文件样式

	// meta
	public boolean ismodify = false;
	public String modifyInfo;
	public int version;
	public int colNum;
	protected MVViewMode viewMode;
	private transient List<ColumnMeta> sortMetas;

	public ColumnMeta getColumnMeta(String name) {
		if (name != null) {
			for (ColumnMeta mcColumnMeta : getColumnMetas()) {
				if (mcColumnMeta != null && name.equals(mcColumnMeta.name)) {
					return (ColumnMeta) mcColumnMeta;
				}
			}
		}
		return null;
	}

	@Override
	public int hashCode() {
		int hashCode = Mc.getHashCode(name, colNum, version, modelName, baseModelName, columnMetas.hashCode());
		return hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		return Mc.equal(this, obj, "name", "colNum", "version", "modelName", "baseModelName");
	}

	public MVViewMode getViewMode() {
		return viewMode;
	}

	public void setViewMode(MVViewMode viewMode) {
		this.viewMode = viewMode;
	}

	public boolean isIsmodify() {
		return ismodify;
	}

	public void setIsmodify(boolean ismodify) {
		this.ismodify = ismodify;
	}

	public String getModifyInfo() {
		return modifyInfo;
	}

	public void setModifyInfo(String modifyInfo) {
		this.modifyInfo = modifyInfo;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public int getColNum() {
		return colNum;
	}

	public void setColNum(int colNum) {
		this.colNum = colNum;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(String primaryKey) {
		this.primaryKey = primaryKey;
	}

	public List<ColumnMeta> getSortColumns() {
		if (sortMetas == null) {
			sortMetas = new ArrayList<ColumnMeta>();
			sortMetas.addAll(columnMetas);
			Collections.sort(sortMetas, new Comparator<ColumnMeta>() {

				@Override
				public int compare(ColumnMeta o1, ColumnMeta o2) {
					return o1.compareTo(o2);
				}
			});
		}
		return sortMetas;
	}

	public Set<ColumnMeta> getColumnMetas() {
		return columnMetas;
	}

	public String getBaseModelName() {
		return baseModelName;
	}

	public void setBaseModelName(String baseModelName) {
		this.baseModelName = baseModelName;
	}

	public String getBaseModelContent() {
		return baseModelContent;
	}

	public void setBaseModelContent(String baseModelContent) {
		this.baseModelContent = baseModelContent;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public String getModelContent() {
		return modelContent;
	}

	public void setModelContent(String modelContent) {
		this.modelContent = modelContent;
	}

	public int getColNameMaxLen() {
		return colNameMaxLen;
	}

	public void setColNameMaxLen(int colNameMaxLen) {
		this.colNameMaxLen = colNameMaxLen;
	}

	public int getColTypeMaxLen() {
		return colTypeMaxLen;
	}

	public void setColTypeMaxLen(int colTypeMaxLen) {
		this.colTypeMaxLen = colTypeMaxLen;
	}

	public int getColDefaultValueMaxLen() {
		return colDefaultValueMaxLen;
	}

	public void setColDefaultValueMaxLen(int colDefaultValueMaxLen) {
		this.colDefaultValueMaxLen = colDefaultValueMaxLen;
	}

}
